package com.yellowpage.bigdata.action;

import java.util.*;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.tool.Excelexport;
import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.bigdata.data.NspOnofflogImeiNewAddDay;
import com.yellowpage.bigdata.service.NspFatallogRequsetSumService;
import com.yellowpage.bigdata.service.NspOnofflogImeiNewAddDayService;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;

public class NspOnofflogImeiNewAddDayAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<NspOnofflogImeiNewAddDay> nspOnofflogImeiNewAddDayList = new ArrayList<NspOnofflogImeiNewAddDay>();
	private List<Map> resultList = new ArrayList<Map>();

	private NspOnofflogImeiNewAddDay nspOnofflogImeiNewAddDay;
	private NspOnofflogImeiNewAddDayService nspOnofflogImeiNewAddDayService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_nspOnofflogImeiNewAddDayId;
	// 标记了查询的起止时间

	private String query_startTime;
	private String query_endTime;
	private String query_createTime;
	private String query_regionCode;
	private String query_channelNo;
	private String query_createUser;
	private String query_note;
	private String query_imeiDayCount;
	private String query_imeiAllCount;
	private String query_imeiNewCount;
	private String query_imsiNewCount;
	private String query_imeiNewAllCount;
	private String query_channelName;

	public String getQuery_imsiNewCount() {
		return query_imsiNewCount;
	}

	public void setQuery_imsiNewCount(String query_imsiNewCount) {
		this.query_imsiNewCount = query_imsiNewCount;
	}

	public String getQuery_imeiNewAllCount() {
		return query_imeiNewAllCount;
	}

	public void setQuery_imeiNewAllCount(String query_imeiNewAllCount) {
		this.query_imeiNewAllCount = query_imeiNewAllCount;
	}

	private RegionService regionService;
	private NspFatallogRequsetSumService nspFatallogRequsetSumService;
	private Map<String, String> regionMap;

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspOnofflogImeiNewAddDay = nspOnofflogImeiNewAddDayService
					.getNspOnofflogImeiNewAddDay(Long.parseLong(selectedId));
		}
	}

	public String getRegionSql() {
		Map session = ActionContext.getContext().getSession();
		User user = (User) session.get("userObject");
		String function_code = "OMC_CLIENT_LOG_COUNT_DAY_NEWADD";
		String regionsql = regionService.getRegionclauseByUserFunction(
				user.getUserId(), function_code, "REGION_CODE");
		return regionsql;
	}

	public String queryChannel() {
		initPageRender();
		regionMap = nspFatallogRequsetSumService.getRegionMap(getRegionSql());
		setPageRenderToRequest();
		return "toRegion";
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();

		queryMap.put("startTime", query_startTime);
		queryMap.put("endTime", query_endTime);
		if(query_channelName!=null){
			queryMap.put("channelName", query_channelName);
		}
		if (!query_regionCode.equals("")) {
			// 是否是管理员
			boolean flag = true;
			regionMap = nspFatallogRequsetSumService
					.getRegionMap(getRegionSql());
			// 画面输入地区代码
			String[] val = query_regionCode.split(";");
			// 地区变量
			String str = "(";
			for (String ss : val) {
				for (String key : regionMap.keySet()) {
					// 如果包含"000000"则为管理员
					if (key.equals("000000")) {
						flag = false;
						break;
					}
				}
				// 如果输入地区代码含有A并且是管理员追加道地区变量后边
				if ("A".equals(ss) && !flag) {
					str += "'" + ss + "',";
					continue;
				}
				for (String key : regionMap.keySet()) {
					if (regionMap.get(key).contains(ss) || key.equals(ss)) {
						str += "'" + key + "',";
						break;
					}
				}
			}
			str = str.substring(0, str.length() - 1) + ")";
			// 如果地区输入的地区代码都不在用户所在地区范围则返回
			if (")".equals(str)) {
				return RESULT_QUERY;
			}
			queryMap.put("regionCode", str);
			// 如果不是管理员
			if (flag) {
				// 用户没有输入渠道号
				if ("".equals(query_channelNo)) {
					// 根据地区编码查询渠道号
					String channel = nspOnofflogImeiNewAddDayService
							.queryChannol(" and REGION_CODE in " + str);
					if ("".equals(channel)) {

						return RESULT_QUERY;
					}
					queryMap.put("channelNo", query_channelNo);
					queryMap.put("channelNo1", channel);
				} else {
					// 用户输入查询渠道号
					String channel = nspOnofflogImeiNewAddDayService
							.queryChannol(" and REGION_CODE in " + str);
					if ("".equals(channel)) {
						return RESULT_QUERY;
					}
					// 用户输入渠道号是否在地区范围之内
					boolean flg = false;
					// 根据用户取得的取到号
					String[] channelNOList = channel.substring(1,
							channel.length() - 1).split(",");
					for (String ss : channelNOList) {
						if (ss.equals(query_channelNo)) {
							flg = true;
						}

					}
					// 用户输入的渠道号跟用户所在地区范围内的渠道号不一致直接返回
					if (!flg) {
						return RESULT_QUERY;
					}
					queryMap.put("channelNo", query_channelNo);
				}
			} else {
				// 如果是管理员，添加用户输入的渠道号
				queryMap.put("channelNo", query_channelNo);
			}
		} else {
			// 当用户没有输入地区代码时
			queryMap.put("regionCode", "");
			String code = getRegionSql();
			// 添加用户所在的地区代码
			queryMap.put("regionCode1", code);
			// 是否是管理员
			boolean flag = true;
			regionMap = nspFatallogRequsetSumService.getRegionMap(code);
			for (String key : regionMap.keySet()) {
				// 如果包含"000000"则为管理员
				if (key.equals("000000")) {
					flag = false;
					break;
				}
			}
			// 如果用户不是管理员
			if (flag) {
				// 用户输入渠道号为空
				if ("".equals(query_channelNo)) {
					// 根据地区代码查询渠道号
					String channel = nspOnofflogImeiNewAddDayService
							.queryChannol(code);
					if ("".equals(channel)) {

						return RESULT_QUERY;
					}
					queryMap.put("channelNo", query_channelNo);
					queryMap.put("channelNo1", channel);
				} else {
					// 用户输入地区代码不为空
					String channel = nspOnofflogImeiNewAddDayService
							.queryChannol(code);
					if ("".equals(channel)) {
						return RESULT_QUERY;
					}
					// 用户输入渠道号是否在地区范围之内
					boolean flg = false;
					// 根据用户取得的取到号
					String[] channelNOList = channel.substring(1,
							channel.length() - 1).split(",");
					for (String ss : channelNOList) {
						if (ss.equals(query_channelNo)) {
							flg = true;
						}

					}
					// 用户输入的渠道号跟用户所在地区范围内的渠道号不一致直接返回
					if (!flg) {
						return RESULT_QUERY;
					}
					queryMap.put("channelNo", query_channelNo);
				}
			} else {
				queryMap.put("channelNo", query_channelNo);
			}
		}

		resultList = nspOnofflogImeiNewAddDayService
				.queryNspOnofflogImeiNewAddDayByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public void exportExcel()

	{
		Map queryMap = new HashMap();

		queryMap.put("startTime", query_startTime);
		queryMap.put("endTime", query_endTime);
		queryMap.put("channelName", query_channelName);

		// queryMap.put("regionCode", query_regionCode);
		if (!query_regionCode.equals("")) {
			// 是否是管理员
			boolean flag = true;
			regionMap = nspFatallogRequsetSumService
					.getRegionMap(getRegionSql());
			// 画面输入地区代码
			String[] val = query_regionCode.split(";");
			// 地区变量
			String str = "(";
			for (String ss : val) {
				for (String key : regionMap.keySet()) {
					// 如果包含"000000"则为管理员
					if (key.equals("000000")) {
						flag = false;
						break;
					}
				}
				// 如果输入地区代码含有A并且是管理员追加道地区变量后边
				if ("A".equals(ss) && !flag) {
					str += "'" + ss + "',";
					continue;
				}
				for (String key : regionMap.keySet()) {
					if (regionMap.get(key).contains(ss) || key.equals(ss)) {
						str += "'" + key + "',";
						break;
					}
				}
			}
			str = str.substring(0, str.length() - 1) + ")";
			// 如果地区输入的地区代码都不在用户所在地区范围则返回
			if (")".equals(str)) {
				resultList = null;
			}
			queryMap.put("regionCode", str);
			// 如果不是管理员
			if (flag) {
				// 用户没有输入渠道号
				if ("".equals(query_channelNo)) {
					// 根据地区编码查询渠道号
					String channel = nspOnofflogImeiNewAddDayService
							.queryChannol(" and REGION_CODE in " + str);
					if ("".equals(channel)) {

						resultList = null;
					}
					queryMap.put("channelNo", query_channelNo);
					queryMap.put("channelNo1", channel);
				} else {
					// 用户输入查询渠道号
					String channel = nspOnofflogImeiNewAddDayService
							.queryChannol(" and REGION_CODE in " + str);
					if ("".equals(channel)) {
						resultList = null;
					}
					// 用户输入渠道号是否在地区范围之内
					boolean flg = false;
					// 根据用户取得的取到号
					String[] channelNOList = channel.substring(1,
							channel.length() - 1).split(",");
					for (String ss : channelNOList) {
						if (ss.equals(query_channelNo)) {
							flg = true;
						}

					}
					// 用户输入的渠道号跟用户所在地区范围内的渠道号不一致直接返回
					if (!flg) {
						resultList = null;
					}
					queryMap.put("channelNo", query_channelNo);
				}
			} else {
				// 如果是管理员，添加用户输入的渠道号
				queryMap.put("channelNo", query_channelNo);
			}
		} else {
			// 当用户没有输入地区代码时
			queryMap.put("regionCode", "");
			String code = getRegionSql();
			// 添加用户所在的地区代码
			queryMap.put("regionCode1", code);
			// 是否是管理员
			boolean flag = true;
			regionMap = nspFatallogRequsetSumService.getRegionMap(code);
			for (String key : regionMap.keySet()) {
				// 如果包含"000000"则为管理员
				if (key.equals("000000")) {
					flag = false;
					break;
				}
			}
			// 如果用户不是管理员
			if (flag) {
				// 用户输入渠道号为空
				if ("".equals(query_channelNo)) {
					// 根据地区代码查询渠道号
					String channel = nspOnofflogImeiNewAddDayService
							.queryChannol(code);
					if ("".equals(channel)) {

						resultList = null;
					}
					queryMap.put("channelNo", query_channelNo);
					queryMap.put("channelNo1", channel);
				} else {
					// 用户输入地区代码不为空
					String channel = nspOnofflogImeiNewAddDayService
							.queryChannol(code);
					if ("".equals(channel)) {
						resultList = null;
					}
					// 用户输入渠道号是否在地区范围之内
					boolean flg = false;
					// 根据用户取得的取到号
					String[] channelNOList = channel.substring(1,
							channel.length() - 1).split(",");
					for (String ss : channelNOList) {
						if (ss.equals(query_channelNo)) {
							flg = true;
						}

					}
					// 用户输入的渠道号跟用户所在地区范围内的渠道号不一致直接返回
					if (!flg) {
						resultList = null;
					}
					queryMap.put("channelNo", query_channelNo);
				}
			} else {
				queryMap.put("channelNo", query_channelNo);
			}
		}

		if (resultList != null) {

			// 给出全量值
			resultList = nspOnofflogImeiNewAddDayService
					.queryNspOnofflogImeiNewAddDayByPage(queryMap, null);
		}
		LinkedHashMap<String, String> namemap = new LinkedHashMap<String, String>();

		// 给出
		namemap.put("LOG_DATE", "日志日期");
		namemap.put("CHANNEL_NO", "渠道号");
		namemap.put("CHANNEL_NAME", "渠道名称 ");
		namemap.put("REGION_CODE", "省份编号");
		namemap.put("REGION_NAME", "省份名称");
		namemap.put("IMEI_DAY_COUNT", "日活跃");
		namemap.put("IMEI_NEW_COUNT", "日新增");
		namemap.put("IMEI_ALL_COUNT", "IMEI累计数");
		namemap.put("IMEI_NEW_ALL_COUNT", "累计新增");
		namemap.put("IMSI", "有IMSI");

		Excelexport.exportExcel(resultList, this.getClass().getName(), namemap);

	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		nspOnofflogImeiNewAddDayService
				.saveNspOnofflogImeiNewAddDay(nspOnofflogImeiNewAddDay);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (nspOnofflogImeiNewAddDay.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspOnofflogImeiNewAddDay.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspOnofflogImeiNewAddDay.getChannelNo().length() == 0) {
			this.addActionError("** ChannelNo 不能为空 !");
		}
		if (nspOnofflogImeiNewAddDay.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}

		this.setValidatorResult("/bigdata/NspOnofflogImeiNewAddDayAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		nspOnofflogImeiNewAddDay = nspOnofflogImeiNewAddDayService
				.getNspOnofflogImeiNewAddDay(id);

		return RESULT_EDIT;
	}

	public String update() {

		nspOnofflogImeiNewAddDayService
				.updateNspOnofflogImeiNewAddDay(nspOnofflogImeiNewAddDay);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (nspOnofflogImeiNewAddDay.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspOnofflogImeiNewAddDay.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspOnofflogImeiNewAddDay.getChannelNo().length() == 0) {
			this.addActionError("** ChannelNo 不能为空 !");
		}
		if (nspOnofflogImeiNewAddDay.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		this.setValidatorResult("/bigdata/NspOnofflogImeiNewAddDayUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.nspOnofflogImeiNewAddDayService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("可能含有子记录约束冲删除失败");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setNspOnofflogImeiNewAddDay(
			NspOnofflogImeiNewAddDay nspOnofflogImeiNewAddDay) {
		this.nspOnofflogImeiNewAddDay = nspOnofflogImeiNewAddDay;
	}

	public NspOnofflogImeiNewAddDay getNspOnofflogImeiNewAddDay() {
		return nspOnofflogImeiNewAddDay;
	}

	public void setNspOnofflogImeiNewAddDayService(
			NspOnofflogImeiNewAddDayService nspOnofflogImeiNewAddDayService) {
		this.nspOnofflogImeiNewAddDayService = nspOnofflogImeiNewAddDayService;
	}

	public List<NspOnofflogImeiNewAddDay> getNspOnofflogImeiNewAddDayList() {
		return nspOnofflogImeiNewAddDayList;
	}

	public void setNspOnofflogImeiNewAddDayList(
			List<NspOnofflogImeiNewAddDay> nspOnofflogImeiNewAddDayList) {
		this.nspOnofflogImeiNewAddDayList = nspOnofflogImeiNewAddDayList;
	}

	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}

	public Map<String, String> getRegionMap() {
		return regionMap;
	}

	public void setRegionMap(Map<String, String> regionMap) {
		this.regionMap = regionMap;
	}

	public NspFatallogRequsetSumService getNspFatallogRequsetSumService() {
		return nspFatallogRequsetSumService;
	}

	public void setNspFatallogRequsetSumService(
			NspFatallogRequsetSumService nspFatallogRequsetSumService) {
		this.nspFatallogRequsetSumService = nspFatallogRequsetSumService;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_nspOnofflogImeiNewAddDayId() {
		return this.query_nspOnofflogImeiNewAddDayId;
	}

	public void setQuery_nspOnofflogImeiNewAddDayId(
			String nspOnofflogImeiNewAddDayId) {
		this.query_nspOnofflogImeiNewAddDayId = nspOnofflogImeiNewAddDayId;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_regionCode() {
		return this.query_regionCode;
	}

	public void setQuery_regionCode(String regionCode) {
		this.query_regionCode = regionCode;
	}

	public String getQuery_channelNo() {
		return this.query_channelNo;
	}

	public void setQuery_channelNo(String channelNo) {
		this.query_channelNo = channelNo;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_imeiDayCount() {
		return this.query_imeiDayCount;
	}

	public void setQuery_imeiDayCount(String imeiDayCount) {
		this.query_imeiDayCount = imeiDayCount;
	}

	public String getQuery_imeiAllCount() {
		return this.query_imeiAllCount;
	}

	public void setQuery_imeiAllCount(String imeiAllCount) {
		this.query_imeiAllCount = imeiAllCount;
	}

	public String getQuery_imeiNewCount() {
		return this.query_imeiNewCount;
	}

	public void setQuery_imeiNewCount(String imeiNewCount) {
		this.query_imeiNewCount = imeiNewCount;
	}

	public String getQuery_startTime() {
		return query_startTime;
	}

	public void setQuery_startTime(String query_startTime) {
		this.query_startTime = query_startTime;
	}

	public String getQuery_endTime() {
		return query_endTime;
	}

	public void setQuery_endTime(String query_endTime) {
		this.query_endTime = query_endTime;
	}

	public String getQuery_channelName() {
		return query_channelName;
	}

	public void setQuery_channelName(String query_channelName) {
		this.query_channelName = query_channelName;
	}

}
